package com.gome.ocean.service.utils.job.reader;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.gome.ocean.common.constants.Constants;
import com.gome.ocean.service.utils.DataXCommonUtils;
import com.gome.ocean.service.utils.job.CreateJobUtils;
import com.google.common.collect.Maps;

/**
 * Created by liuqingxia on 2016/2/1.
 */
public class HdfsReader {
    public static final String[] MAP_KEYS         = new String[]{"path","defaultFS","columns","fileType","encoding","fieldDelimiter"};
    public static final String[] CAN_BE_NULL_KEYS = new String[]{"encoding","fieldDelimiter"};

    public static Map<String,Object> getReaderMap(HttpServletRequest req){
        Map<String,Object> readerMaps = Maps.newHashMap();
        for (String key : MAP_KEYS){
            String paramName = CreateJobUtils.addPrefix(Constants.REQUEST_READER_PARAM_PRE, key);
            String paramValue = req.getParameter(paramName);
            if (StringUtils.isBlank(paramValue)){
                if (DataXCommonUtils.keyCanBeNull(key, CAN_BE_NULL_KEYS,req)){
                    continue;
                }else {
                    throw new IllegalArgumentException("参数" +paramName + "不能为空");
                }
            }
            if ("columns".equals(key)){
                String[] columnValues = paramValue.split(Constants.REG_CN_OR_EN_COMMA);
                readerMaps.put(key,columnValues);
                continue;
            }
            readerMaps.put(key,paramValue);
        }
        return readerMaps;
    }

}
